package com.project.movie.controllers;


import com.project.movie.entities.Actor;
import com.project.movie.entities.Movies;
import com.project.movie.entities.Roles;
import com.project.movie.services.ActorService;
import com.project.movie.services.MovieService;
import com.project.movie.services.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("actors")
public class ActorControllers {

    @Autowired
    ActorService actorService;
    @Autowired
    RoleService roleService;
    @Autowired
    MovieService movieService;

    @GetMapping
    public String displayActor(Model model){
        List<Actor> actors = actorService.getAll();
        model.addAttribute("actors",actors);
        return "list-actor";
    }

    @GetMapping("/new")
    public String displayActorForm(Model model){

        Actor aActor=new Actor();
        List<Roles> roles=roleService.getAll();
        List<Movies> movies = movieService.getAll();
        model.addAttribute("actor",aActor);
        model.addAttribute("roles",roles);
        return "new-actor";
    }

    @PostMapping("/save")
    public String creatActor(Model model, @RequestParam(value = "roles",required = false) List<Long> roles, Actor actor){
        actorService.save(actor);
        return "redirect:/actors";
    }

}
